package com.app.medicalcontrol.fragment.User.child;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import com.app.medicalcontrol.R;
import com.app.medicalcontrol.basis.BasisFragment;
import com.app.medicalcontrol.basis.Constants;
import com.app.medicalcontrol.bean.BasicBeanStr;
import com.app.medicalcontrol.bean.LoginBean;
import com.app.medicalcontrol.bean.UpdateBean;
import com.app.medicalcontrol.login.LoginActivity;
import com.app.medicalcontrol.other.NewsH5DetailActivity;
import com.app.medicalcontrol.util.glide.GlideCatchUtil;
import com.bumptech.glide.Glide;
import com.flyco.dialog.listener.OnBtnClickL;
import com.loopj.android.http.RequestParams;
import butterknife.BindView;
import butterknife.OnClick;
import my.DialogUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/***
 * 设置
 * **/
public class SettingFragment extends BasisFragment {

    @BindView(R.id.text_cache)
    TextView text_cache;
    @BindView(R.id.btn_exit)
    Button exitBtn;

    @Override
    public void initViews() {
        // TODO Auto-generated method stub
        super.initViews();
        setContentView(R.layout.fragment_system_set);
        setTitle(getString(R.string.system_set));
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.initData(savedInstanceState);
        setCacheView();
    }

    void setCacheView(){
        text_cache.setText(GlideCatchUtil.getInstance().getCacheSize());
    }


    @OnClick(R.id.view_clear)
    void clear() {
        showLoading();
        Glide.get(mContext).clearMemory();
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... strings) {
                GlideCatchUtil.getInstance().cleanCacheDisk();
                GlideCatchUtil.getInstance().clearCacheDiskSelf();
                GlideCatchUtil.getInstance().clearCacheMemory();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                hideLoading();
                showToast(getString(R.string.setting_clear_cache_success));
                setCacheView();
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
        }.execute("");
    }

    @OnClick(R.id.view_aboutus)
    void aboutUs() {
        NewsH5DetailActivity.toDetail(mContext);
    }

    @OnClick(R.id.view_update)
    void update() {
        UpdateBean.check(mContext, true);
    }

    @OnClick(R.id.btn_exit)
    void exit() {
        DialogUtils.DialogTwo(mContext, "", "是否退出登录？", "退出", "取消", new OnBtnClickL() {
            @Override
            public void onBtnClick() {
//				LoginBean.logout(mContext);
//				btn_logout.setVisibility(View.GONE);
                LoginBean.refreshToken(new LoginBean.Token_Listener() {
                    @Override
                    public void onSuccess() {
                        postLogout();
                        LoginActivity.toLoadingAllClear(mContext);
                    }

                    @Override
                    public void onFailure() {

                    }

                    @Override
                    public void onFinish() {

                    }
                });
            }
        }, null);
    }

    void postLogout() {
        RequestParams params = new RequestParams();
        params.put("cticket", LoginBean.getInstance().getTicket().getValue());
        params.put("ctoken", LoginBean.getUserToken());
        showLoadingDialog();
        HttpRestClient.get(Constants.URL_LOGOUT, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        LoginBean.logout(mContext);
                        finish();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                }, 0,
                BasicBeanStr.class);
    }

}
